package org.code.mallspringboot.controller;

import org.code.mallspringboot.dto.FlashSessionDto;
import org.code.mallspringboot.pojo.SmsFlashPromotionSession;
import org.code.mallspringboot.service.SmsFlashPromotionSessionService;
import org.code.mallspringboot.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/flashSession")
public class SmsFlashPromotionSessionController {

    @Autowired
    private SmsFlashPromotionSessionService smsFlashPromotionSessionService;

    //数据列表展示
    @GetMapping("/list")
    public CommonResult list(){
        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionService.list();
        if (!list.isEmpty()){
            return CommonResult.success(list);
        }
        return CommonResult.failed("SmsFlashPromotionSession查询失败");
    }

    //启用/禁用
    @PostMapping("/update/status/{id}")
    public CommonResult update_status(@PathVariable int id,int status){
        if (smsFlashPromotionSessionService.update_status(id, status) > 0){
            return CommonResult.success("修改成功!");
        }
        return CommonResult.failed("修改失败!");
    }

    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable int id, @RequestBody FlashSessionDto flashSessionDto){
        flashSessionDto.setId(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        flashSessionDto.setStartTime2(simpleDateFormat.format(flashSessionDto.getStartTime()));
        flashSessionDto.setEndTime2(simpleDateFormat.format(flashSessionDto.getEndTime()));
        if (smsFlashPromotionSessionService.update(flashSessionDto) > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @PostMapping("/create")
    public CommonResult create(@RequestBody FlashSessionDto flashSessionDto){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        flashSessionDto.setStartTime2(simpleDateFormat.format(flashSessionDto.getStartTime()));
        flashSessionDto.setEndTime2(simpleDateFormat.format(flashSessionDto.getEndTime()));
        if (smsFlashPromotionSessionService.create(flashSessionDto) > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败!");
    }

    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable int id){
        if (smsFlashPromotionSessionService.delete(id) > 0){
            return CommonResult.success("删除成功!");
        }
        return CommonResult.failed("删除失败!");
    }

    @GetMapping("/selectList")
    public CommonResult selectList(int flashPromotionId){
        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionService.selectList(flashPromotionId);
        if (!list.isEmpty()){
            return CommonResult.success(list);
        }
        return CommonResult.failed("查询失败");
    }
}
